import Vue from 'vue'
import VueRouter from 'vue-router'

// 路由懒加载
const Login = () => import(/* Login_Home_Welcome */'../compoents/Login.vue')
const Home = () => import(/* Login_Home_Welcome */'../compoents/Home.vue')
const Welcome = () => import(/* Login_Home_Welcome */'../compoents/welcome.vue')

const Users = () => import(/* Users_Rights_Role */'../compoents/user/Users.vue')
const Right = () => import(/* Users_Rights_Role */'../compoents/power/right.vue')
const Role = () => import(/* Users_Rights_Role */'../compoents/power/role.vue')

const Cate = () => import(/* Cate_Params_List_Add */'../compoents/goods/Cate.vue')
const Params = () => import(/* Cate_Params_List_Add */'../compoents/goods/params.vue')
const List = () => import(/* Cate_Params_List_Add */'../compoents/goods/List.vue')
const Add = () => import(/* Cate_Params_List_Add */'../compoents/goods/Add.vue')

const Order = () => import(/* Order_Report */'../compoents/order/order.vue')
const Report = () => import(/* Order_Report */'../compoents/report/report.vue')

// import Login from '../compoents/Login.vue'
// import Home from '../compoents/Home.vue'
// import Welcome from '../compoents/welcome.vue'
// import Users from '../compoents/user/Users.vue'
// import Right from '../compoents/power/right.vue'
// import Role from '../compoents/power/role.vue'
// import Cate from '../compoents/goods/Cate.vue'
// import Params from '../compoents/goods/params.vue'
// import List from '../compoents/goods/List.vue'
// import Add from '../compoents/goods/Add.vue'
// import Order from '../compoents/order/order.vue'
// import Report from '../compoents/report/report.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Right },
      { path: '/roles', component: Role },
      { path: '/categories', component: Cate },
      { path: '/params', component: Params },
      { path: '/goods', component: List },
      { path: '/goods/add', component: Add },
      { path: '/orders', component: Order },
      { path: '/reports', component: Report }
    ]
  }

]

const router = new VueRouter({
  routes
})

// 导航守位 beforeEach
router.beforeEach((to, from, next) => {
  // 如果要去登录页面,直接放行
  if (to.path === '/login') return next()
  const tokenStr = sessionStorage.getItem('token')
  // 如果token能取到值,说明已经登录,就放行,如果没有,就去登录页面
  if (!tokenStr) return next('/login')
  next()
})

export default router
